package com.yupi.springbootinit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.manager.AiManager;
import com.yupi.springbootinit.manager.RedisLimiterManager;
import com.yupi.springbootinit.mapper.ChartMapper;
import com.yupi.springbootinit.mapper.UserMapper;
import com.yupi.springbootinit.mapper.YouxiMapper;
import com.yupi.springbootinit.model.entity.Chart;
import com.yupi.springbootinit.model.entity.Youxi;
import com.yupi.springbootinit.service.ChartService;
import com.yupi.springbootinit.service.UserService;
import com.yupi.springbootinit.service.YouxiService;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
* @author Lenovo
* @description 针对表【youxi】的数据库操作Service实现
* @createDate 2024-04-05 23:02:17
*/
@Service
@Slf4j
public class YouxiServiceImpl extends ServiceImpl<YouxiMapper, Youxi> implements YouxiService{

    private  long biModelId = 1659171950288818178L;
    @Resource
    private UserService userService;
    @Resource
    private AiManager aiManager;
    @Resource
    private RedisLimiterManager redisLimiterManager;

    @Autowired
    private YouxiMapper youxiMapperl;
    @Autowired
    private ChartService chartService;
    @Autowired
    private ChartMapper chartMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public String tableToChart(String startTime,String endTime) {
        //从数据库中查出想要的数据行
        String tableName="oCNlP4-z6j9mP6Wo7C4ahnAZDgLw";
        QueryWrapper qw=new QueryWrapper();
        qw.eq("userAccount",tableName);
        List<Map<String,Object>> list = this.listMaps(qw);
        log.info(String.valueOf(list));



        StringBuilder sb=new StringBuilder();
        sb.append("游戏时间，游戏得分，游戏耗时，游戏难度").append("\n");

        //使用迭代器，挨个取值 插入chart表的chartdata字段中
        for (Map<String, Object> next : list) {
            log.info("原表中的数据："+next.toString());

            //拼接字符串
            Object gameTime = next.get("gameTime");
            Object gameScore = next.get("gameScore");
            Object gameConsume = next.get("gameconsume");
            Object gameLevel = next.get("gameLevel");
            sb.append(gameTime).append(",");
            sb.append(gameScore).append(",");
            sb.append(gameConsume).append(",");
            sb.append(gameLevel).append("\n");


        }
        String resultFix = sb.toString();
        System.out.println(resultFix);
        String goal="这是一个阿尔兹海默症患者使用康复游戏进行康复训练的训练数据，包含了游戏时间，游戏得分，游戏耗时，游戏难度（难度数值越大说明游戏难度越高），帮我分析一下病人的现在的情况如何？";
        String name="阿尔兹海默症康复训练结果图";
        String chartType="柱状图";

        StringBuilder userInput= new StringBuilder();
        userInput.append("分析需求：").append("\n");

        // 拼接分析目标
        String userGoal = goal;
        if (StringUtils.isNotBlank(chartType)) {
            userGoal += "，请使用" + chartType;
        }
        userInput.append(userGoal).append("\n");
        // 压缩后的数据
        userInput.append("原始数据：").append("\n");
        userInput.append(resultFix).append("\n");

        System.out.println(userInput);

        String result = aiManager.doChat(biModelId, userInput.toString());
        String[] splits = result.split("【【【【【");
        System.out.println(result);
        if (splits.length < 3) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "AI 生成错误");
        }
        String genChart = splits[1].trim();
        String genResult = splits[2].trim();

        //准备插入到数据库中
        Chart chart = new Chart();
        chart.setName(name);
        chart.setGoal(goal);
        chart.setChartData(resultFix);
        chart.setChartType(chartType);
        chart.setGenChart(genChart);
        chart.setGenResult(genResult);

        boolean saveResult = chartService.save(chart);





        return null;
    }
}




